package com.cyzy.mq;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cyzy.dto.ResponseDto;
import com.cyzy.pojo.Coupon;
import com.cyzy.pojo.CouponSeckill;
import com.cyzy.pojo.UserCoupons;
import com.cyzy.service.CouponKillService;
import com.cyzy.service.CouponService;
import com.cyzy.service.UserCouponsService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;

@Component
@RocketMQMessageListener(topic = "coupon_kill",consumerGroup = "consumer-demo1")
public class MQConsumer implements RocketMQListener<CouponSeckill> {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private CouponKillService couponKillService;

    @Autowired
    private CouponService couponService;

    @Autowired
    private UserCouponsService userCouponsService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

//    @Override
//    public void onMessage(String s) {  //有消息过来，会自动回调onMessage
//
//        System.out.println("接收到消息：" +s);
//        //业务代码
//    }

    @Override
    public void onMessage(CouponSeckill couponSeckill) {
        System.out.println(couponSeckill+"========along=======");

        Coupon coupon = couponService.selectCoupon(couponSeckill.getCouponId());
        System.out.println(coupon+"----------coupon----------");
//        if(coupon == null){
//            //购买失败就移除用户
//            redisTemplate.opsForSet().remove(setKey,userId);
//            //再把redis优惠卷加回去
//            redisTemplate.opsForList().leftPush(couponKillKey,s);
//            return ResponseDto.fail("获取订单失败！");
//        }
        //获取当前时间
        Timestamp now = new Timestamp(System.currentTimeMillis());
        // 计算毫秒数（1天=24*60*60*1000毫秒）
        long millisecondsToAdd = coupon.getValidDays() * 24L * 60 * 60 * 1000;
        Timestamp newTimestamp = new Timestamp(now.getTime() + millisecondsToAdd);
        //生成我的订单
        UserCoupons userCoupons = new UserCoupons();
        userCoupons.setUserId(Long.parseLong(couponSeckill.getUserId()));
        userCoupons.setTemplateId(couponSeckill.getCouponId());
        userCoupons.setExpireTime(newTimestamp);

        //生成订单
        userCouponsService.save(userCoupons);
        //减秒杀优惠券数量
        LambdaUpdateWrapper<CouponSeckill> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CouponSeckill::getUserId, couponSeckill.getUserId())
                .eq(CouponSeckill::getCouponId, couponSeckill.getCouponId()) // 通常还需要ID条件
                .gt(CouponSeckill::getSeckillNum, 0) // 确保数量大于0才减
                .setSql("seckill_num = seckill_num - 1"); // 直接使用SQL表达式减1

        // 执行更新
        boolean updated = couponKillService.update(updateWrapper);

    }
}
